package com.edu.hre.db.sql.recruit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreCandidateLicense;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.recruit.CanLicenseForm;
import com.edu.hre.struts.form.recruit.CanLicenseListForm;

public class CanLicenseDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "canLicenseDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<CanLicenseForm> getCanLicenseList(int limit, int offset, int canId){

		ArrayList<CanLicenseForm> listResult = new ArrayList<CanLicenseForm>();

		String sql = "SELECT clic.id AS col1," +
					" clic.license_no AS col2," +
					" lic.name AS col3," +
					" clic.license_issued_date AS col4, " +
					" clic.license_expiry_date AS col5 " +

					" FROM hre_candidate_license AS clic" +
					" LEFT OUTER JOIN hre_license AS lic ON clic.license_id = lic.id" +
					" LEFT OUTER JOIN hre_job_candidate AS can ON clic.candidate_id = can.id " +

					" WHERE can.is_deleted = 0 " +
					" AND can.id = ? " +
					" ORDER BY clic.date_created DESC";
		
		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, canId);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				CanLicenseForm license = new CanLicenseForm();
	
				license.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				license.setCanId(canId);
				license.setLicenseNo(CommonUtils.normalizeObjectToString(objs[2], ""));
				license.setLicenseType(CommonUtils.normalizeObjectToString(objs[3], ""));
				
				try {
					license.setIssuedDate(CommonUtils.dateToString((Date) objs[4]));
					license.setExpiryDate(CommonUtils.dateToString((Date) objs[5]));
				} catch (Exception e) {
					CommonUtils.println(e.getMessage());
				}

				listResult.add(license);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return listResult;
	}

	public int countCanLicenses(int canId){

		int count = baseService.countByColumns(new String[]{"candidateId"}, new int[]{Compare.EQUAL}, new Integer[]{canId}, "id", HreCandidateLicense.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLicenses(CanLicenseListForm form){

		ArrayList<CanLicenseForm> info = (ArrayList<CanLicenseForm>) form.getAllLicenses();

		for (int i = 0; i < info.size(); i++) {
			
			try {

				CanLicenseForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreCandidateLicense.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
